import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../components/Login.vue";
import Home from "../components/Home.vue";
import Welcome from "../components/Welcome.vue";
import Users from "../components/managePage/Users.vue";
import Song from "../components/managePage/Song";
import Collect from "../components/managePage/Collect";
import  Authorization from "../components/managePage/Authorization";
import Comment from "../components/managePage/Comment";
import Index from "../views/Index.vue";
import Singer from "../views/Singer";
import MyMusic from "../views/MyMusic";
import CollectPage from "../views/CollectPage";


Vue.use(VueRouter);

const routes = [
  {
    path: "/home",
    name: "Home",
    component: Home,
    redirect: "/welcome",
    children: [
      { path: "/welcome", name: "Welcome", component: Welcome },
      { path: "/user", name: "Users", component: Users },
      { path: "/song", name: "Song", component: Song },
      { path: "/collect", name: "Collect", component: Collect },
      { path: "/comment", name: "Comment", component: Comment },
      {
        path:"authorization",
        name:"Authorization",
        component:Authorization
      }
    ]
  },
  {
    path: "/login",
    name: "Login",
    component: Login
  },
  {
    path: "/index",
    name: "Index",
    component: Index
  },
  {
    path: "/singer",
    name: "Singer",
    component: Singer
  },
  {
    path: "/myMusic",
    name: "MyMusic",
    component: MyMusic
  },
  {
    path: "/collectPage",
    name: "CollectPage",
    component: CollectPage
  },

  {
    path: "/",
    redirect: "/index"
  }
];

const router = new VueRouter({
  routes
});

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === "/login") return next();
  if (to.path === "/index") return next();
  const tokenStr = window.sessionStorage.getItem("token");
  if (!tokenStr && to.path === "/home") return next("/login");
  next();
});

export default router;
